<template>
<div class="product-add">
    <!--form表单-->
    <el-form size="small" ref="form" :model="form" label-width="200px">
      <div class="common-seach-wrap">
        <div class="common-form">{{$t('appsetting.payment_settings')}}</div>
        <el-form-item :label="$t('appsetting.select_payment_method')">
          <div class="paytype">
            <pay-type-selection
              :form="form"
              :platform="platform"
              :pay-type="payType"
            ></pay-type-selection>
          </div>
        </el-form-item>
      </div>

      <el-tabs v-model="activeName">
        <el-tab-pane :label="$t('appsetting.wechat_pay_settings')" name="wx">
          <el-form-item :label="$t('appsetting.wechat_withdrawal_method')">
            <div>
              <el-radio v-model="form.wxCashType" :label="2"
                >{{$t('appsetting.withdraw_to_balance')}}</el-radio
              >
              <el-radio v-model="form.wxCashType" :label="3"
                >{{$t('appsetting.merchant_transfer_to_balance')}}</el-radio
              >
              <el-radio v-model="form.wxCashType" :label="4"
                >{{$t('appsetting.merchant_transfer')}}</el-radio
              >
              <div class="tips">{{$t('appsetting.wechat_note')}}</div>
            </div>
          </el-form-item>
          <el-form-item :label="$t('appsetting.wechat_mchid')">
            <el-input v-model="form.mchid" class="max-w460"></el-input>
          </el-form-item>
          <el-form-item :label="$t('appsetting.wechat_apikey')">
            <el-input v-model="form.apikey" class="max-w460"></el-input>
          </el-form-item>
          <el-form-item :label="$t('appsetting.wechat_sign_type')">
            <div>
              <el-radio v-model="form.wxSignType" :label="0">{{$t('appsetting.certificate')}}</el-radio>
              <el-radio v-model="form.wxSignType" :label="1">{{$t('appsetting.public_key')}}</el-radio>
            </div>
          </el-form-item>
          <el-form-item :label="$t('appsetting.wechat_cert_serial_id')">
            <el-input
              :rows="4"
              :placeholder="$t('appsetting.cert_serial_note')"
              v-model="form.wechatpaySerial"
              class="max-w460"
            ></el-input>
            <!-- <div class="tips">
              微信商户平台-API安全-V3平台证书-序列号,将序列号的全部内容复制进来
            </div> -->
          </el-form-item>
          <template v-if="form.wxSignType === 1">
            <el-form-item :label="$t('appsetting.payment_pubkey_file')">
              <el-input
                type="textarea"
                :rows="4"
                v-model="form.pubKeyPem"
                :placeholder="$t('appsetting.pubkey_copy_tip1')"
                class="max-w460"
              ></el-input>
              <div class="tips"> {{$t('appsetting.pubkey_copy_tip1')}} 

</div>
            </el-form-item>
          </template>
          <el-form-item label="apiclient_cert.pem">
            <el-input
              type="textarea"
              :rows="4"
              :placeholder="$t('appsetting.apiclient_cert_copy1')"
              v-model="form.certPem"
              class="max-w460"
            ></el-input>
            <div class="tips"> {{$t('appsetting.apiclient_cert_copy1')}} 

</div>
          </el-form-item>
          <el-form-item label="apiclient_key.pem">
            <el-input
              type="textarea"
              :rows="4"
              :placeholder="$t('appsetting.apiclient_key_copy1')"
              v-model="form.keyPem"
              class="max-w460"
            ></el-input>
            <div class="tips"> {{$t('appsetting.apiclient_key_copy1')}} 

</div>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane :label="$t('appsetting.alipay_settings')" name="ali">
          <el-form-item :label="$t('appsetting.alipay_appid')">
            <el-input v-model="form.alipayAppid" class="max-w460"></el-input>
          </el-form-item>
          <el-form-item :label="$t('appsetting.alipay_withdrawal_method')">
            <div>
              <el-radio v-model="form.aliCashType" :label="1"
                >{{$t('appsetting.manual_payment')}}</el-radio
              >
              <el-radio v-model="form.aliCashType" :label="2"
                >{{$t('appsetting.auto_transfer_on_approval')}}</el-radio
              >
              <div class="tips"> {{$t('appsetting.alipay_auto_note')}} 

</div>
            </div>
          </el-form-item>
          <el-form-item :label="$t('appsetting.alipay_app_pub_cert_path')">
            <el-input
                type="textarea"
                :rows="4"
                v-model="form.aliAppCertPublicKey"
                class="max-w460"
            ></el-input>
          </el-form-item>
          <el-form-item :label="$t('appsetting.alipay_pub_cert_path')">
            <el-input
                type="textarea"
                :rows="4"
                v-model="form.alipayCertPublicKey"
                class="max-w460"
            ></el-input>
          </el-form-item>
          <el-form-item :label="$t('appsetting.alipay_root_cert_path')">
            <el-input
                type="textarea"
                :rows="4"
                v-model="form.alipayRootCert"
                class="max-w460"
            ></el-input>
          </el-form-item>
          <el-form-item :label="$t('appsetting.alipay_private_key')">
            <el-input
              type="textarea"
              :rows="4"
              v-model="form.alipayPrivatekey"
              class="max-w460"
            ></el-input>
          </el-form-item>
        </el-tab-pane>
      </el-tabs>

      <!--提交-->
      <div class="common-button-wrapper">
        <el-button type="primary" @click="onSubmit">{{$t('appsetting.submit')}}</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import AppSettingApi from "@/api/appsetting.js";
import PayTypeSelection from "./part/paytype.vue";
import { formatModel } from "@/utils/base.js";
export default {
  components: {
    PayTypeSelection
  },
  data() {
    return {
      form: {
        payType: [{}],
        mchid: "",
        apikey: "",
        alipayAppid: "",
        alipayPublickey: "",
        alipayPrivatekey: "",
        wxCashType: 2,
        aliCashType: 1,
        p12: null,
        wxPayKind: 3,
        certPem: "",
        keyPem: "",
        wechatpaySerial: "",
        wxSignType: 0,
        pubKeyPem: "",
        aliAppCertPublicKey: "",
        alipayCertPublicKey: "",
        alipayRootCert: ""
      },
      app: {},
      payType: [],
      platform: [],
      activeName: "wx"
    };
  },
  created() {
    this.getData();
  },
  methods: {
    /*获取数据*/
    getData() {
      let self = this;
      AppSettingApi.payDetail({}, true).then(res => {
        self.app = res.data.app;
        self.payType = res.data.payType;
        self.platform = res.data.platform;
        self.form = formatModel(self.form, res.data.app);
        console.log("payType", self.payType);
        if (self.app.payTypeJson && self.app.payTypeJson !== null) {
          self.form.payType = JSON.parse(JSON.stringify(self.app.payTypeJson));
          Object.getOwnPropertyNames(self.form.payType).forEach(function (key) {
            for (let i = 0; i < self.form.payType[key].payType.length; i++) {
              self.form.payType[key].payType[i] = parseInt(self.form.payType[key].payType[i]);
            }
          });
        } else {
          // self.form.payType = deepClone(res.data.platform);
          self.form.payType = JSON.parse(JSON.stringify(res.data.platform));
        }
      }).catch(error => {
        console.log(error);
      });
    },
    //提交表单
    onSubmit() {
      let self = this;
      let params = self.form;
      params.wxPayKind = 3;
      if (params.payType.length < 1) {
        ElMessage({
          message: $t("appsetting.payment_method_required"),
          type: "warning"
        });
        return;
      }
      AppSettingApi.editPay(params, true).then(data => {
        ElMessage({
          message: $t("appsetting.setup_success"),
          type: "success"
        });
      }).catch(error => {});
    },
    /*选择上传图片*/
    fileChange(e) {
      console.log(e);
    },
    /*选择图片之前*/
    onBeforeUploadP12(file) {
      return true;
    },
    /*上传图片*/
    UploadP12(param) {
      let self = this;
      const loading = ElLoading.service({
        lock: true,
        text: $t("appsetting.file_uploading"),
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      const formData = new FormData();
      formData.append("iFile", param.file);
      AppSettingApi.uploadP12(formData).then(response => {
        loading.close();
        ElMessage({
          message: $t("appsetting.file_upload_success"),
          type: "success"
        });
        param.onSuccess(); // 上传成功的图片会显示绿色的对勾
      }).catch(response => {
        loading.close();
        ElMessage({
          message: $t("appsetting.file_upload_fail"),
          type: "warning"
        });
        param.onError();
      });
    }
  }
};
</script>

<style scoped>
.paytype {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.common-seach-wrap {
  background: #f6f6f6;
  margin-bottom: 12px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 16px 16px 0 16px;
}
</style>

